﻿using Microsoft.AspNetCore.Http;
using System.Net.Http;
using XUCore.Extensions;

namespace XUCore.Webs
{
    /// <summary>
    /// multipart/form-data 多文件上传构造类
    /// </summary>
    public class UrlMultipartFormDataContent
    {
        private MultipartFormDataContent _multipartFormDataContent;
        /// <summary>
        /// multipart/form-data 多文件上传构造类
        /// </summary>
        public UrlMultipartFormDataContent()
        {
            _multipartFormDataContent = new MultipartFormDataContent();
        }

        /// <summary>
        ///  multipart/form-data MIME type.
        /// </summary>
        public MultipartFormDataContent MultipartFormDataContent
        {
            get
            {
                return _multipartFormDataContent;
            }
        }
        /// <summary>
        /// 初始化创建一个 multipart/form-data MIME type.
        /// </summary>
        /// <returns></returns>
        public static UrlMultipartFormDataContent Create() => new UrlMultipartFormDataContent();
        /// <summary>
        /// 写入HttpContent see： multipart/form-data MIME type.
        /// </summary>
        /// <param name="content">内容 参照：<see cref="StringContent"/>或<see cref="ByteArrayContent"/>或<see cref="System.Net.Http.FormUrlEncodedContent"/></param>
        /// <returns></returns>
        public UrlMultipartFormDataContent Add(HttpContent content)
        {
            _multipartFormDataContent.Add(content);

            return this;
        }
        /// <summary>
        /// 写入HttpContent see： multipart/form-data MIME type.
        /// </summary>
        /// <param name="content">内容 参照：<see cref="StringContent"/>或<see cref="ByteArrayContent"/>或<see cref="System.Net.Http.FormUrlEncodedContent"/></param>
        /// <param name="name">要添加的HTTP内容的名称，后端用该名字获取文件对象</param>
        /// <returns></returns>
        public UrlMultipartFormDataContent Add(HttpContent content, string name)
        {
            _multipartFormDataContent.Add(content, name);

            return this;
        }
        /// <summary>
        /// 写入HttpContent see： multipart/form-data MIME type.
        /// </summary>
        /// <param name="content">内容 参照：<see cref="StringContent"/>或<see cref="ByteArrayContent"/>或<see cref="System.Net.Http.FormUrlEncodedContent"/></param>
        /// <param name="name">要添加的HTTP内容的名称，后端用该名字获取文件对象</param>
        /// <param name="fileName">要添加到集合的HTTP内容的文件名</param>
        /// <returns></returns>
        public UrlMultipartFormDataContent Add(HttpContent content, string name, string fileName)
        {
            _multipartFormDataContent.Add(content, name, fileName);

            return this;
        }
        /// <summary>
        /// 写入字段 see： multipart/form-data MIME type.
        /// </summary>
        /// <param name="content">内容 <see cref="StringContent"/></param>
        /// <param name="name">要添加的HTTP内容的名称，后端用该名字获取内容</param>
        /// <returns></returns>
        public UrlMultipartFormDataContent Add(string name, object content)
        {
            _multipartFormDataContent.Add(new StringContent(content.SafeString()), name);

            return this;
        }
        /// <summary>
        /// 写入字段 see： multipart/form-data MIME type.
        /// </summary>
        /// <param name="condition">是否写入</param>
        /// <param name="content">内容 <see cref="StringContent"/></param>
        /// <param name="name">要添加的HTTP内容的名称，后端用该名字获取内容</param>
        /// <returns></returns>
        public UrlMultipartFormDataContent Add(bool condition, string name, object content)
        {
            if (!condition) return this;

            _multipartFormDataContent.Add(new StringContent(content.SafeString()), name);

            return this;
        }
        /// <summary>
        /// 写入文件 see： multipart/form-data MIME type.
        ///     <para>例如：</para>
        ///     <para>using var streamContent = new StreamContent(file.OpenReadStream());</para>
        ///     <para>var buffers = await streamContent.ReadAsByteArrayAsync();</para>
        /// </summary>
        /// <param name="buffers">文件流 <see cref="ByteArrayContent"/></param>
        /// <param name="name">要添加的HTTP内容的名称，后端用该名字获取文件对象</param>
        /// <param name="fileName">要添加到集合的HTTP内容的文件名</param>
        /// <returns></returns>
        public UrlMultipartFormDataContent AddFile(string name, string fileName, byte[] buffers)
        {
            _multipartFormDataContent.Add(new ByteArrayContent(buffers), name, fileName);

            return this;
        }
        /// <summary>
        /// 写入文件 see： multipart/form-data MIME type.
        ///     <para>例如：</para>
        ///     <para>using var streamContent = new StreamContent(file.OpenReadStream());</para>
        ///     <para>var buffers = await streamContent.ReadAsByteArrayAsync();</para>
        /// </summary>
        /// <param name="condition">是否写入</param>
        /// <param name="buffers">文件流 <see cref="ByteArrayContent"/></param>
        /// <param name="name">要添加的HTTP内容的名称，后端用该名字获取文件对象</param>
        /// <param name="fileName">要添加到集合的HTTP内容的文件名</param>
        /// <returns></returns>
        public UrlMultipartFormDataContent AddFile(bool condition, string name, string fileName, byte[] buffers)
        {
            if (!condition) return this;

            _multipartFormDataContent.Add(new ByteArrayContent(buffers), name, fileName);

            return this;
        }
        /// <summary>
        /// 写入文件 see： multipart/form-data MIME type.
        /// </summary>
        /// <param name="name">要添加的HTTP内容的名称，后端用该名字获取文件对象</param>
        /// <param name="formFile">文件对象 <see cref="IFormFile"/></param>
        /// <returns></returns>
        public UrlMultipartFormDataContent AddFile(string name, IFormFile formFile)
        {
            using var streamContent = new StreamContent(formFile.OpenReadStream());

            var buffers = streamContent.ReadAsByteArrayAsync().GetAwaiter().GetResult();

            _multipartFormDataContent.Add(new ByteArrayContent(buffers), name, formFile.FileName);

            return this;
        }
        /// <summary>
        /// 写入文件 see： multipart/form-data MIME type.
        /// </summary>
        /// <param name="name">要添加的HTTP内容的名称，后端用该名字获取文件对象</param>
        /// <param name="fileName">要添加到集合的HTTP内容的文件名</param>
        /// <param name="downloadUrl">远程文件对象 <see cref="IFormFile"/></param>
        /// <returns></returns>
        public UrlMultipartFormDataContent AddFile(string name, string fileName, string downloadUrl)
        {
            var client = new HttpClient();

            var buffers = client.GetByteArrayAsync(downloadUrl).GetAwaiter().GetResult();

            _multipartFormDataContent.Add(new ByteArrayContent(buffers), name, fileName);

            return this;
        }
        /// <summary>
        /// 写入文件 see： multipart/form-data MIME type.
        /// </summary>
        /// <param name="condition">是否写入</param>
        /// <param name="formFile">文件对象 <see cref="IFormFile"/></param>
        /// <param name="name">要添加的HTTP内容的名称，后端用该名字获取文件对象</param>
        /// <returns></returns>
        public UrlMultipartFormDataContent AddFile(bool condition, string name, IFormFile formFile)
        {
            if (!condition) return this;

            using var streamContent = new StreamContent(formFile.OpenReadStream());

            var buffers = streamContent.ReadAsByteArrayAsync().GetAwaiter().GetResult();

            _multipartFormDataContent.Add(new ByteArrayContent(buffers), name, formFile.FileName);

            return this;
        }
    }
}
